<template>
    <div class="app-container">
        <div class="filter-container">
            <el-form :model="queryParams"  :inline="true">

                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </div>

        <el-table  v-loading="loading" :data="dataList"  fit  highlight-current-row style="width: 100%;">
            <el-table-column prop="companyName" align="center" label="租户名称"  />
            <el-table-column prop="loginName" align="center" label="登录名"  />
            <el-table-column prop="userName" align="center" label="姓名"  />
            <el-table-column prop="email" align="center" label="邮箱"  />
            <el-table-column prop="mobile" align="center" label="手机"  />
            <el-table-column prop="qqNum" align="center" label="QQ号"  />
            <el-table-column prop="wxNum" align="center" label="微信号"  />
            <el-table-column prop="begDate" align="center" label="开始时间"  />
            <el-table-column prop="endDate" align="center" label="结束时间"  />
            <el-table-column prop="status"  :formatter="statusFormat" align="center" label="状态"  />
            <el-table-column label="更新时间" align="center" prop="updateDate">
                <template slot-scope="scope">
                    <span>{{ scope.row.updateDate | dateFormat('YYYY-MM-DD HH:mm') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" >
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                    >修改</el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination  :total="total" :page.sync="queryParams.current"  :limit.sync="queryParams.size" @pagination="getList"  />

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="租户名称" prop="companyName">
                            <el-input v-model="form.companyName" placeholder="请输入租户名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="登录名" prop="loginName">
                            <el-input v-model="form.loginName" placeholder="请输入租户管理登录名" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="密码" prop="password">
                            <el-input v-model="form.password" type="password" placeholder="请输入密码" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="姓名" prop="userName">
                            <el-input v-model="form.userName" placeholder="请输入姓名" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="邮箱" prop="email">
                            <el-input v-model="form.email" placeholder="请输入邮箱" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="电话" prop="phone">
                            <el-input v-model="form.phone" placeholder="请输入电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="手机" prop="mobile">
                            <el-input v-model="form.mobile" placeholder="请输入手机" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="QQ号" prop="qqNum">
                            <el-input v-model="form.qqNum" placeholder="请输入qq号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="微信号" prop="wxNum">
                            <el-input v-model="form.wxNum" placeholder="请输入微信号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="开始时间" prop="begDate">
                            <!-- <el-input v-model="form.begDate" placeholder="请输入开始时间" /> -->
                             <el-date-picker
                                v-model="begEndDate"
                                type="dateRange"
                                format="yyyy-MM-dd HH:mm"
                                value-format="yyyy-MM-dd HH:mm"
                                range-separator="至"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="12">
                        <el-form-item label="结束时间" prop="endDate">
                            <el-input v-model="form.endDate" placeholder="请输入结束时间" />
                        </el-form-item>
                    </el-col> -->
                   <el-col :span="12">
                        <el-form-item label="状态" prop="status">
                            <el-select v-model="form.status" placeholder="请选择">
                                <el-option
                                v-for="item in statusDict"
                                :key="item.value"
                                :label="item.name"
                                :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="租户id" prop="tenantId">
                            <el-input v-model="form.tenantId" placeholder="请输入租户id" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

    </div>
</template>


<script>
    import Pagination from '@/components/Pagination'
    export default {
        name: 'SysTenant',
        components: { Pagination },
        data() {
            return {
                // 弹出层标题
                title: '',
                // 遮罩层
                loading: true,
                // 总数量
                total: 0,
                // 列表数据
                dataList: [],
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    current: 1,
                    size: 10,
                },
                 // 显示状态数据字典
                statusDict: [{
                    value: 0,
                    name: '开启'
                },
                {
                    value: 1,
                    name: '禁用'
                }],
                // 表单参数
                form: {

                },
                // 表单校验
                rules: {
                    companyName: [
                        { required: true, message: '租户名称不能为空', trigger: 'blur' }
                    ],
                    userName: [
                        { required: true, message: '用户名称不能为空', trigger: 'blur' }
                    ],
                    loginName: [
                        { required: true, message: '登录名称不能为空', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '密码不能为空', trigger: 'blur' }
                    ],
                    email: [
                    {
                        type: "email",
                        message: "请输入正确的邮箱地址",
                        trigger: ["blur", "change"]
                    }
                    ],
                    mobile: [
                    { required: true, message: "手机号码不能为空", trigger: "blur" },
                        {
                            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                            message: "请输入正确的手机号码",
                            trigger: "blur"
                        }
                    ]
      
            
                }
            }
        },
        created() {
            this.getList()
        },
        methods: {
             // 状态字典翻译
            statusFormat(row, column) {
                let obj = this.statusDict.find(dict => dict.value == row.status)
                return obj.name
            },
            /** 查询列表 */
            getList() {
                this.$store.dispatch('admin/sysTenant/findPage', this.queryParams)
                    .then((data) => {
                    this.dataList = data.records
                    this.total = data.total
                    this.loading = false
                })
            .catch((err) => {
                    console.log('失败' + err)
                })
            },
            // 取消按钮
            cancel() {
                this.open = false
                this.reset()
            },
            // 表单重置
            reset() {
                this.form = {
                        companyName: undefined,
                        loginName: undefined,
                        password: undefined,
                        userName: undefined,
                        email: undefined,
                        phone: undefined,
                        mobile: undefined,
                        qqNum: undefined,
                        wxNum: undefined,
                        begEndDate: [new Date(), new Date()],
                        begDate: undefined,
                        endDate: undefined,
                        status: 0,
                        tenantId: undefined,
                }
                this.$nextTick(()=>{
                    this.$refs['form'].resetFields();
                })
            },
            /** 新增按钮操作 */
            handleAdd(row) {
                this.reset()
                this.open = true
                this.title = '添加'
            },
            /** 删除按钮操作 */
            handleDel(id) {
                this.$store.dispatch('admin/sysTenant/delById', id).then(() => {
                    this.getList()
                    this.msg('删除成功')
                })
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset()
                this.$store.dispatch('admin/sysTenant/getById', row.id)
                    .then((data) => {
                    this.form = data
                    this.open = true
                    this.title = '修改'
                }).catch((err) => {
                    console.log('失败' + err)
                })
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.id !== undefined) {
                            this.$store.dispatch('admin/sysTenant/updateById', this.form).then(data => {
                                this.msg('修改成功')
                                this.open = false
                                this.getList()
                            })
                        } else {
                            this.$store.dispatch('admin/sysTenant/saveData', this.form).then(res => {
                                this.msg('新增成功')
                                this.open = false
                                this.getList()
                            })
                        }
                    }
                })
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                this.$confirm('是否确认删除名称为"' + row.id + '"的数据项?', '警告', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.handleDel(row.id)
                })
            }
        }
    }
</script>
